<template>
  <div :class="className" :style="{ height: height, width: width }" />
</template>

<script>
import * as echarts from "echarts";
require("echarts/theme/macarons"); // echarts theme
import resize from "./mixins/resize";
import { getDashboard2 } from "@/api/dashboard";
export default {
  mixins: [resize],
  props: {
    className: {
      type: String,
      default: "chart",
    },
    width: {
      type: String,
      default: "100%",
    },
    height: {
      type: String,
      default: "260px",
    },
  },
  data() {
    return {
      chart: null,
      data: [
        { value: 320, name: "Industries" },
        { value: 240, name: "Technology" },
        { value: 149, name: "Forex" },
        { value: 100, name: "Gold" },
        { value: 59, name: "Forecasts" },
      ],
      title: ["Industries", "Technology", "Forex", "Gold", "Forecasts"],
    };
  },
  mounted() {
    this.$nextTick(() => {
      this.getData(new Object());
    });
  },
  created() {},
  beforeDestroy() {
    if (!this.chart) {
      return;
    }
    this.chart.dispose();
    this.chart = null;
  },
  methods: {
    getData(query) {
      getDashboard2(query).then((response) => {
        this.data = response.data;
        this.title = this.data.map((item) => item.name);
        if (this.chart) {
         this.chart.dispose();
        this.chart = null;
        }
        
        this.initChart();
      });
    },
    initChart() {
      this.chart = echarts.init(this.$el, "macarons");

      var d = this.data;

      var b = {
        tooltip: {
          trigger: "item",
        },
        legend: {
          top: "5%",
          left: "center",
        },
        series: [
          {
            name: "",
            type: "pie",
            radius: ["40%", "70%"],
            avoidLabelOverlap: false,
            itemStyle: {
              borderRadius: 10,
              borderColor: "#fff",
              borderWidth: 2,
            },
            label: {
              show: false,
              position: "center",
            },
            emphasis: {
              label: {
                show: true,
                fontSize: 40,
                fontWeight: "bold",
              },
            },
            labelLine: {
              show: false,
            },
            data: d,
          },
        ],
      };

      this.chart.setOption(b);
    },
  },
};
</script>
